<?php
	if(@$_FILES['img']['type'] != '')
	{
		$arr_type = array('image/gif', 'image/jpeg', 'image/png', 'image/x-png', 'image/pjpeg', 'image/bmp');
		if(in_array($_FILES['img']['type'], $arr_type))
		{
			$img_name = $global->create_ramdom_string(20);
			$upload_name = explode('.', $_FILES['img']['name']);
			$ext = end($upload_name);
			move_uploaded_file($_FILES['img']['tmp_name'], '../source_pics/'.$img_name.'.'.$ext);
			echo '<script type="text/javascript">showAlert(\'Thông báo\', \'Upload ảnh thành công\', \'\')</script>';
		}
		else
		{
			echo '<script type="text/javascript">showAlert(\'Thông báo\', \'Định dạng file không đúng\', \'\')</script>';
		}
	}
	
	@$path = $_GET['path'];
	@$act = $_GET['act'];
	if($path != '' && $act == 'del')
	{
		unlink($path);
	}
?>
<script type="text/javascript">
	function checkSubmit(){
		if(document.upload.img.value == ''){
			showAlert('Thông báo', 'Vui lòng chọn ảnh để upload', 'img');
			return false;
		}
		return true;
	}
	
	function delImage(url){
		window.location = url;
	}
</script>
<form name="upload" method="post" enctype="multipart/form-data" action="admin_master_page.php?cmd=10" onsubmit="return checkSubmit()">
	<table class="form-layout">
    	<tr>
        	<td width="70">Chọn ảnh</td>
            <td><input type="file" name="img" id="img" /></td>
            <td><input type="submit" value="Upload" /></td>
        </tr>
    </table>
</form>
<table class="table-list" width="90%">
	<tr class="table-header"><td>STT</td><td>URL</td><td>Preview</td><td>Xóa</td></tr>
    <?php
		$no = 0;
    	foreach (glob("../source_pics/*.{jpg,jpeg,gif,png,bmp}", GLOB_BRACE) as $filename)
		{
			$no++;
			echo '<tr>';
			echo '<td align="center">'.$no.'</td>';
			echo '<td>http://tranhtheunamphuong.com'.str_replace('..','',$filename).'</td>';
			echo '<td align="center"><img src="'.$filename.'" width="100"></td>';
			echo '<td align="center"><a href="javascript:void()"><img src="img/ico_delete.png" title="Xóa" onclick="showConfirm(\'Xác nhận\', \'Bạn có chắc muốn xóa ảnh này không?\', \'delImage([admin_master_page.php?cmd=10&path='.$filename.'&act=del])\')" /></a></td>';
			echo '</tr>';
		}
	?>
</table>